package com.wink_172.library.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.wink_172.library.R;


public class ActionBarView extends RelativeLayout implements View.OnClickListener {


    private ImageView left_icon, right_icon;
    private TextView left_text, content_view, right_view;
    private View line_view;
    private RelativeLayout lef_contain;

    public void setCallback(ICallback callback) {
        this.mCallback = callback;
    }

    public ICallback mCallback;

    public interface ICallback {
        void onSendEvent(int event, String content);
    }


    public ActionBarView(Context context) {
        super(context);
    }

    public ActionBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ActionBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();


        initView();
    }

    public static final int LEFT_BTN = 0;
    public static final int RIGHT_BTN = 1;

    @Override
    public void onClick(View v) {
        if (mCallback == null) {
            return;
        }
        if (v.getId() == R.id.lef_contain) {
            mCallback.onSendEvent(LEFT_BTN, null);

        } else if (v.getId() == R.id.right_icon || v.getId() == R.id.right_view) {
            mCallback.onSendEvent(RIGHT_BTN, null);
        }

    }

    private void initView() {
        left_text = findViewById(R.id.left_text);
        lef_contain = findViewById(R.id.lef_contain);
        left_icon = findViewById(R.id.left_icon);
        content_view = findViewById(R.id.content_view);
        right_view = findViewById(R.id.right_view);
        line_view = findViewById(R.id.line_view);
        right_icon = findViewById(R.id.right_icon);

        lef_contain.setOnClickListener(this);
        right_view.setOnClickListener(this);
        right_icon.setOnClickListener(this);

//        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
//        layoutParams.topMargin = ConmonUtils.getStatusBarHeight(getContext());
    }

    public void initialized(int type, int flags, Object... args) {
//        RelativeLayout loadView = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.action_bar_view, null);
//        removeAllViews();
//        addView(loadView);
    }

    public void updateAllContent(int leftIcon, String text, String rightText) {
        left_icon.setImageResource(leftIcon);
        content_view.setText(text);
        right_view.setText(rightText);
    }

    public void updateAllContent2(int leftIcon, String text, String rightText, String leftText) {
        left_icon.setImageResource(leftIcon);
        content_view.setText(text);
        right_view.setText(rightText);
        left_text.setText(leftText);
    }

    public void updateAllContent3(int leftIcon, String text, int rightIcon) {
        left_icon.setImageResource(leftIcon);
        content_view.setText(text);
        right_view.setText("");
        right_icon.setImageResource(rightIcon);
    }


    public void updateAllContent4(int leftIcon, String text, int rightIcon, int centerIcon) {
        left_icon.setImageResource(leftIcon);
        content_view.setText(text);
        Drawable drawable = getContext().getResources().getDrawable(centerIcon);
        // 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        content_view.setCompoundDrawables(drawable, null, null, null);

        right_view.setText("");
        right_icon.setImageResource(rightIcon);
    }

    public void updateAllContent5(int leftIcon, String text, Drawable rightRes, String rightText) {
        left_icon.setImageResource(leftIcon);
        content_view.setText(text);
        right_view.setText(rightText);
        right_view.setBackground(rightRes);

        LayoutParams layoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        layoutParams.rightMargin = 10;
        right_view.setLayoutParams(layoutParams);
        right_view.setPadding(24, 12, 24, 12);

    }

    public void setLineEnable(boolean enable) {
        line_view.setVisibility(enable ? VISIBLE : INVISIBLE);
//        content_view.setTextColor(getResources().getColor(R.color.white));
    }

    public void updateContent(String text) {
        content_view.setText(text);
    }


}
